package com.vickw.springcloud.controller;

import com.vickw.springcloud.entity.CommonResult;
import com.vickw.springcloud.entity.Payment;
import com.vickw.springcloud.service.IPaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @version 1.0
 * @Description：
 * @Author VickW
 * @Date 2022/2/16 23:21
 */
@RestController   //必须是这个注解，因为是模拟前后端分离的restful风格的请求，要求每个方法返回 json
@Slf4j
public class PaymentController {

    @Resource
    private IPaymentService paymentService;

    @Value("${server.port}")
    private String serverPort;

    @Resource  // 服务发现
    private DiscoveryClient discoveryClient;

    @PostMapping(value = "/payment/create")
    //	    注意这里的 @RequestBody  是必须要写的，虽然 MVC可以自动封装参数成为对象，
    //      但是当消费者项目调用，它传参是 payment 整个实例对象传过来的， 即Json数据，因此需要写这个注解
    // https://blog.csdn.net/weixin_38004638/article/details/99655322
    public CommonResult create(@RequestBody Payment payment) {   //@RequestBody 加上注解之后传参就不能只传serial
        int result = paymentService.creatPayment(payment);
        log.info("****插入结果：" + result);
        if (result > 0) {
            return new CommonResult(200, "插入数据库成功,serverPort: " + serverPort, result);
        }
        return new CommonResult(444, "插入数据库失败,serverPort: " + serverPort, null);
    }

    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        Payment result = paymentService.getPaymentById(id);
        log.info("****查询结果：" + result);
        if (result != null) {
            return new CommonResult(200, "查询成功,serverPort: " + serverPort, result);
        }
        return new CommonResult(444, "没有对应id的记录,serverPort: " + serverPort, null);
    }

    @GetMapping("/payment/discovery")
    public Object getDiscovery() {
        // 获取服务清单
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("***********services:" + service);
        }

        // 根据具体服务进一步获得该微服务的信息
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PROVIDER-PAYMENT");
        for (ServiceInstance instance : instances) {
            log.info(instance.getInstanceId() + "\t" + instance.getServiceId() + "\t" +
                    instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        }

        return this.discoveryClient;
    }

    @GetMapping(value = "payment/lb")
    public String getPaymentLB(){
        return serverPort;
    }

    @GetMapping(value = "payment/feign/timeout")
    public String paymentFeignTimeout(){
        /*暂停几秒钟线程，模拟提供方超时的情况*/
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

}

